package org.bluedream.core.module.liandi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.liandi.entity.Member;
import org.bluedream.core.module.liandi.service.MemberService;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.bluedream.core.utils.sms.Sms;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MemberController
 * @Description TODO
 * @Author Administrator
 * @Data 2021/12/16 9:08
 * @Version
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/member")
public class MemberController extends BaseController {
    @Resource
    private MemberService memberService;

    @ModelAttribute
    public Member get(String phone){
        return memberService.getById(phone);
    }

    /**
     * 会员明细
     * @param entity
     * @param model
     * @return
     */
    @RequiresPermissions("member:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Member entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("member" , entity);
        return "module/member/memberList";
    }

    /**
     * 群发短信
     * @param phones
     * @param model
     * @return
     */
    @RequiresPermissions("member:view")
    @RequestMapping("/form")
    public String form(String[] phones, Model model){
        model.addAttribute("signName" , Sms.SIGN_NAME);
        model.addAttribute("sms" , memberService.initSms(phones));
        return "module/member/memberForm";
    }

    @RequiresPermissions("member:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Member> findPage(@RsaSecurityParameter Member entity){
        return memberService.findPage(entity);
    }

    @RequiresPermissions("member:view")
    @RequestMapping("/sendBatchSms")
    @ResponseBody
    public RestResult sendBatchSms(Sms entity){
        System.out.println("sendBatchSms");
        return memberService.sendBatchSms(entity);
    }

    @RequiresPermissions("member:view")
    @RequestMapping("/importForm")
    public String importSmsPhones(Model model){
        return "module/member/importForm";
    }

    /**
     * 导入excel演示
     * @param file
     */
    @RequiresPermissions("member:view")
    @RequestMapping(value = "/excelImport")
    @ResponseBody
    public RestResult excelImport(@RequestParam("file") MultipartFile file){
        return memberService.sendBatchSms(file);
    }
}